package com.chenjf.userservice;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class UserController {
    @Autowired
    private RestTemplate restTemplate;
//    @Autowired
//    private LoadBalancerClient loadBalancerClient;
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(RestTemplateBuilder restTemplateBuilder){
        return restTemplateBuilder.build();
    }


    @GetMapping("userOrders")
    public String userOrders(){
//        ServiceInstance serviceInstance = loadBalancerClient.choose("spring-cloud-order-service");
//        String url = String.format("http://%s:%s",serviceInstance.getHost(),serviceInstance.getPort());
        return  restTemplate.getForObject("http://spring-cloud-order-service/userOrderList",String.class);
    }
}
